/*
 * @Description: 抓取vova平台数据
 * @Author: zhangzhengzhe
 * @LastEditors: Please set LastEditors
 * @Date: 2019-03-22 11:50:23
 * @LastEditTime: 2019-03-22 13:03:04
 */
const puppeteer = require("puppeteer")
const myApp = async () => {
  const width = 1920
  const height = 938
  const browser = await puppeteer.launch({
    headless: false, // 开启界面
    devtools: true,  // 开启开发者控制台
    // slowMo: 250
  });
  const page = await browser.newPage();
  await page.goto("http://ebay.sfc.com", { waitUntil: 'load' });
  // 监听console输出
  page.on('console', msg => {
    for (let i = 0; i < msg.args.length; ++i)
      console.log(`${i}: ${msg.args[i]}`);
  });
  // 登录
  await page.type("input[name='username']", "zhangzhengzhe");
  await page.type("input[name='password']", "123456");
  await page.click(".el-button.el-button--primary");
  await page.waitFor(2000);
  // 进入广告管理
  await page.goto("http://ebay.sfc.com/ebay/advertising", { waitUntil: "load" });
  await page.waitFor(2000);
  const tableList = await page.evaluate(() => {
    const table = document.querySelectorAll(".el-table table.el-table__body tbody tr")
    console.log(table)
    return table
  })
  console.log(tableList)
  await page.click(".header-box .left-row button:nth-child(2)");
  await page.waitForNavigation({waitUntil: "load"});
}
myApp()


const selected = [
  {
    name: "Albania",
    freight: 22.22
  }, {
    title: "Angola",
    freight: 11.22
  }, {
    title: "Antigua and Barbuda",
    freight: 33.55
  }, {
    title: "Aruba",
    freight: 60.56
  }];
const appFn  = async () => {
  // 不可隆
  const notCloneFn = async () => {
    await page.evaluate(() => {
      const $ = window.$;
      const tableList = $("#prodlist-table tbody tr");
      console.log(tableList);
      tableList.map((index, item) => {
        $(item).find("td").eq(6).click();
      });
      $("#batch-not-clone").click();
    });
  }

  if (await page.$("#modifySaleCategory")) {
    await page.click("#modifySaleCategory");
    await page.waitFor(3000);
    await page.goto("https://merchant.vova.com.hk/index.php?q=admin/main/cloneProduct/pendingCloneProductList&perPage=50");
    await page.waitFor(3000);
    await notCloneFn();
    await page.waitFor(3000);
    await page.goto("http://merchant.vova.com.hk/index.php?q=admin/main/product/index&perPage=100");
  }
}